package com.lishuang.product.service.impl;

import com.lishuang.basic.exception.BusinessException;
import com.lishuang.basic.service.IBaiduAiAuditService;
import com.lishuang.basic.service.impl.BaseServiceImpl;
import com.lishuang.basic.util.LoginContext;
import com.lishuang.org.domain.Employee;
import com.lishuang.org.mapper.EmployeeMapper;
import com.lishuang.product.domain.Product;
import com.lishuang.product.domain.ProductDetail;
import com.lishuang.product.domain.ProductOnlineAuditLog;
import com.lishuang.product.mapper.ProductDetailMapper;
import com.lishuang.product.mapper.ProductMapper;
import com.lishuang.product.mapper.ProductOnlineAuditLogMapper;
import com.lishuang.product.service.IProductService;
import com.lishuang.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * 服务管理合并服务详情保存
     */
    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 服务管理合并服务详情删除
     */
    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    /**
     * 服务管理合并服务详情pi批量删除
     */
    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
     * 服务管理合并服务详情修改
     */
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }
    /**
     * 服务管理上架
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //获取登录信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
            if (textBoo && imageBoo) {
                product.setOnsaletime(new Date());
                product.setState(1L);
                //上架成功，修改上架时间和状态
                productMapper.update(product);

                //添加审核信息表
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setAuditId(employee.getId());
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote("审核通过");
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
            } else {

                //添加审核信息表
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setState(-1);
                productOnlineAuditLog.setAuditId(employee.getId());
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote("审核失败，数据违规。。。");
                productOnlineAuditLogMapper.save(productOnlineAuditLog);

                throw new BusinessException("数据违规。。。");
            }
        }
    }


    /**
     * 服务管理下架
     */
    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        productMapper.offsale(offsaletime, ids);
    }
}
